package cn.wei.youvie.service.impl;

import cn.wei.youvie.common.exception.BusinessException;
import cn.wei.youvie.common.exception.ThrowUtils;
import cn.wei.youvie.common.servlet.DeleteRequest;
import cn.wei.youvie.common.servlet.ErrorCode;
import cn.wei.youvie.domain.dto.content.ContentAddRequest;
import cn.wei.youvie.domain.dto.content.ContentUpdateRequest;
import cn.wei.youvie.domain.entity.Book;
import cn.wei.youvie.domain.entity.User;
import cn.wei.youvie.domain.vo.ContentVO;
import cn.wei.youvie.mapper.BooksMapper;
import cn.wei.youvie.service.BookService;
import cn.wei.youvie.service.SectionService;
import cn.wei.youvie.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wei.youvie.domain.entity.Content;
import cn.wei.youvie.service.ContentService;
import cn.wei.youvie.mapper.ContentsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
* @author Youvie
* @description 针对表【contents(存储节或段落中的具体内容)】的数据库操作Service实现
* @createDate 2024-08-27 21:02:15
*/
@Service
public class ContentServiceImpl extends ServiceImpl<ContentsMapper, Content>
    implements ContentService {

    @Autowired
    private UserService userService;

    @Autowired
    @Lazy
    private BookService bookService;


    @Override
    public Integer add(ContentAddRequest contentsAddRequest, HttpServletRequest request) {

        isAuthUpdateBySection(request, contentsAddRequest.getSectionId());

        Content content = new Content();
        BeanUtils.copyProperties(contentsAddRequest, content);
        this.save(content);
        return content.getContentId();
    }

    @Override
    public boolean deleteContent(DeleteRequest deleteRequest, HttpServletRequest request) {
        isAuthUpdateByContent(deleteRequest.getId(), request);

        return this.removeById(deleteRequest.getId());
    }

    @Override
    public boolean updateContent(ContentUpdateRequest contentsUpdateRequest, HttpServletRequest request) {
        isAuthUpdateByContent(Long.valueOf(contentsUpdateRequest.getContentId()), request);
        return false;
    }

    /**
     * 是否书本创建人或者管理员
     */
    private void isAuthUpdateByContent(Long contentId, HttpServletRequest request) {
        Content content = this.getById(contentId);
        ThrowUtils.throwIf(content == null, ErrorCode.NOT_FOUND_ERROR);

        isAuthUpdateBySection(request, content.getSectionId());
    }

    /**
     * 是否书本创建人或者管理员
     */
    private void isAuthUpdateBySection(HttpServletRequest request, int sectionId) {
        Book book = bookService.getBookBySectionId(sectionId);
        ThrowUtils.throwIf(book == null, ErrorCode.NOT_FOUND_ERROR);

        User loginUser = userService.getLoginUser(request);
        if (!Objects.equals(book.getUserId(), loginUser.getId()) && userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
    }

    @Override
    public ContentVO getContentVO(Content content, HttpServletRequest request) {
        return null;
    }
}




